package com.chinawlx.wlxteacher.utils;

import android.app.Activity;

import com.chinawlx.wheelview.OptionsPickerView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * 作者:黄帅
 * 时间:16/9/14
 * 描述:滚轮工具类
 */
public class WLXWheelViewUtils {

    public static final int COUNT = 30;//调课往后推最多30天
    public static ArrayList<String> shows = new ArrayList<>();//日月数据
    public static ArrayList<String> pres = new ArrayList<>();//年份数据,上传服务器准备

    private WLXWheelViewUtils() {

    }

    /**
     * 获取到最低日期后推一个月的时间集合
     *
     * @param lowTime 秒
     * @return 集合:数据
     */
    public static void setTimes(Long lowTime) {
        Calendar calendar = Calendar.getInstance();
        //TODO 为了优化体验，目前最低日期先从第二天开始，以后根据实际需求再换
        calendar.add(Calendar.DAY_OF_MONTH,1);
//        calendar.setTimeInMillis(lowTime * 1000);//当前课时间为最低时间
//        calendar.add(Calendar.MONTH, 1);//拿到一个月后的当前日期
        for (int i = 0; i < COUNT; i++) {
            int monthNum = calendar.get(Calendar.MONTH) + 1;
            int dayNum = calendar.get(Calendar.DAY_OF_MONTH);
            String month = monthNum + "";
            String day = dayNum + "";
            if (monthNum < 10) {
                month = "0" + monthNum;
            }
            if (dayNum < 10) {
                day = "0" + dayNum;
            }

            String pre = calendar.get(Calendar.YEAR) + "-" + month + "-" + day;
            pres.add(pre);
            String show = month + "月" + day + "日";
            shows.add(show);

            //当前天数加1
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

    }

    private static OptionsPickerView pvOptions;

    public static void showOneWheel(Activity activity, ArrayList<String> options1Items, String
            title, int
                                            selectPosition, OptionsPickerView
                                            .OnOptionsSelectListener listener) {
        pvOptions = new OptionsPickerView(activity);
        pvOptions.setPicker(options1Items);
        pvOptions.setTitle(title);
        //设置是否循环滚动
        pvOptions.setCyclic(false);
        //设置选中的item位置
        pvOptions.setSelectOptions(selectPosition);
        pvOptions.setOnoptionsSelectListener(listener);
        pvOptions.show();
    }

    public static void showTwoWheel(Activity activity, long lowTime,

                                    String title, int
                                            selectPosition1, int
                                            selectPosition2, OptionsPickerView
                                            .OnOptionsSelectListener listener) {
        setTimes(lowTime);

        pvOptions = new OptionsPickerView(activity);
        ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
        for (int i = 0; i < COUNT; i++) {
            ArrayList<String> simple = new ArrayList<>();

            simple.add("05:00-06:00");
            simple.add("06:00-07:00");
            simple.add("07:00-08:00");
            simple.add("08:00-09:00");
            simple.add("09:00-10:00");
            simple.add("10:00-11:00");
            simple.add("11:00-12:00");
            simple.add("12:00-13:00");
            simple.add("13:00-14:00");
            simple.add("14:00-15:00");
            simple.add("15:00-16:00");
            simple.add("16:00-17:00");
            simple.add("17:00-18:00");
            simple.add("18:00-19:00");
            simple.add("19:00-20:00");
            simple.add("20:00-21:00");
            simple.add("21:00-22:00");

            options2Items.add(simple);
        }

        pvOptions.setPicker(shows, options2Items, true);
        pvOptions.setTitle(title);
        //设置是否循环滚动
        pvOptions.setCyclic(false, false, false);
        //设置选中的item位置
        pvOptions.setSelectOptions(selectPosition1, selectPosition2);
        pvOptions.setOnoptionsSelectListener(listener);
        pvOptions.show();
    }

    //==================班级加课,显示加课时间,周几几点,自动添加一小时========================

    public static ArrayList<String> weeks = new ArrayList<>();//周,某一天
    public static ArrayList<ArrayList<String>> hours = new ArrayList<>();//小时
    public static ArrayList<ArrayList<ArrayList<String>>> mins = new ArrayList<>();//分钟

    /**
     * 专门固定用来显示星期几
     *
     * @param activity
     * @param title           标题
     * @param selectPosition1 显示周几
     * @param selectPosition2 显示几点
     * @param selectPosition3 几点钟
     * @param listener        监听器
     */
    public static void showThreeWheel(Activity activity, ArrayList<String> showWeeks,
                                      String title, int
                                              selectPosition1, int
                                              selectPosition2, int
                                              selectPosition3, OptionsPickerView
                                              .OnOptionsSelectListener listener) {
        weeks = new ArrayList<>();
        hours = new ArrayList<>();
        mins = new ArrayList<>();

        if (showWeeks == null) {
            initWeeksData();
        } else {
            weeks.addAll(showWeeks);
        }
        initHoursData();
        initMinsData();

        pvOptions = new OptionsPickerView(activity);
        pvOptions.setPicker(weeks, hours, mins, false);
        pvOptions.setTitle(title);
        //设置是否循环滚动
        pvOptions.setCyclic(false, false, false);
        //设置选中的item位置
        pvOptions.setSelectOptions(selectPosition1, selectPosition2, selectPosition3);
        pvOptions.setOnoptionsSelectListener(listener);
        pvOptions.show();
    }

    //七个
    private static void initWeeksData() {
        weeks.add("星期天");
        weeks.add("星期一");
        weeks.add("星期二");
        weeks.add("星期三");
        weeks.add("星期四");
        weeks.add("星期五");
        weeks.add("星期六");
    }

    // 7-24
    private static void initHoursData() {
        for (int i = 0; i < weeks.size(); i++) {
            ArrayList<String> hour = new ArrayList<>();
            hour.add("00");
            hour.add("01");
            hour.add("02");
            hour.add("03");
            hour.add("04");
            hour.add("05");
            hour.add("06");
            hour.add("07");
            hour.add("08");
            hour.add("09");
            int tep = 10;
            while (tep < 24) {
                hour.add(tep + "");
                tep++;
            }
            hours.add(hour);
        }
    }

    //7-24-12
    private static void initMinsData() {
        for (int i = 0; i < weeks.size(); i++) {
            //第一层
            ArrayList<ArrayList<String>> min2 = new ArrayList<>();
            for (int j = 0; j < 24; j++) {
                //第二层数据
                ArrayList<String> min1 = new ArrayList<>();
                min1.add("00");
                min1.add("05");
                int tep = 10;
                while (tep < 60) {
                    min1.add(tep + "");
                    tep += 5;
                }
                min2.add(min1);
            }
            mins.add(min2);
        }
    }

    /**
     * 获取到开始的时间
     *
     * @param hoursPosition
     * @param minsPosition
     * @return
     */
    public static String getStartTime(int weekPosition, int hoursPosition, int minsPosition) {
        String hour = hours.get(weekPosition).get(hoursPosition);
        String min = mins.get(weekPosition).get(hoursPosition).get(minsPosition);
        return hour + ":" + min + ":00";
    }

    /**
     * 获取到结束的时间
     *
     * @param hoursPosition 方法内会自动获取到往后推一个小时的数据
     * @param minsPosition
     * @return
     */
    public static String getEndTime(int weekPosition, int hoursPosition, int minsPosition) {
        hoursPosition++;
//        minsPosition++;
        if (hoursPosition >= 24) {
            hoursPosition = 0;
        }
        if (minsPosition >= 12) {
            minsPosition = 0;
        }
        String hour = hours.get(weekPosition).get(hoursPosition);
        String min = mins.get(weekPosition).get(hoursPosition).get(minsPosition);
        return hour + ":" + min + ":00";
    }

    //==========================================

    public static void dismiss() {
        if (pvOptions == null) return;
        if (pvOptions.isShowing()) {
            pvOptions.dismiss();
        }
    }

    public static boolean isShowing() {
        if (pvOptions == null) return false;
        return pvOptions.isShowing();
    }

    /**
     * 获取选择到的开始时间 05:00:00
     *
     * @param first   前面滚轮的位置
     * @param sencond 后面滚轮的位置
     * @return
     */
    public static String getOpenTimeString(int first, int sencond) {
        ArrayList<String> simple = new ArrayList<>();

        simple.add("05:00:00");
        simple.add("06:00:00");
        simple.add("07:00:00");
        simple.add("08:00:00");
        simple.add("09:00:00");
        simple.add("10:00:00");
        simple.add("11:00:00");
        simple.add("12:00:00");
        simple.add("13:00:00");
        simple.add("14:00:00");
        simple.add("15:00:00");
        simple.add("16:00:00");
        simple.add("17:00:00");
        simple.add("18:00:00");
        simple.add("19:00:00");
        simple.add("20:00:00");
        simple.add("21:00:00");
        String time = pres.get(first) + " " + simple.get(sencond);
        return time;
    }

    /**
     * 获得结束时间 06:00:00
     *
     * @param first   前面滚轮的位置
     * @param sencond 后面滚轮的位置
     * @return
     */
    public static String getCloseTimeString(int first, int sencond) {
        ArrayList<String> simple = new ArrayList<>();

        simple.add("06:00:00");
        simple.add("07:00:00");
        simple.add("08:00:00");
        simple.add("09:00:00");
        simple.add("10:00:00");
        simple.add("11:00:00");
        simple.add("12:00:00");
        simple.add("13:00:00");
        simple.add("14:00:00");
        simple.add("15:00:00");
        simple.add("16:00:00");
        simple.add("17:00:00");
        simple.add("18:00:00");
        simple.add("19:00:00");
        simple.add("20:00:00");
        simple.add("21:00:00");
        simple.add("22:00:00");
        String time = pres.get(first) + " " + simple.get(sencond);
        return time;
    }

    /**
     * 获取到时间 05:00-06:00
     *
     * @param position
     * @return
     */
    public static String getTimeString(int position) {
        ArrayList<String> simple = new ArrayList<>();

        simple.add("05:00-06:00");
        simple.add("06:00-07:00");
        simple.add("07:00-08:00");
        simple.add("08:00-09:00");
        simple.add("09:00-10:00");
        simple.add("10:00-11:00");
        simple.add("11:00-12:00");
        simple.add("12:00-13:00");
        simple.add("13:00-14:00");
        simple.add("14:00-15:00");
        simple.add("15:00-16:00");
        simple.add("16:00-17:00");
        simple.add("17:00-18:00");
        simple.add("18:00-19:00");
        simple.add("19:00-20:00");
        simple.add("20:00-21:00");
        simple.add("21:00-22:00");


        if (simple.size() < position) return "";
        return simple.get(position);
    }

    /**
     * 获取到二级目录的显示数据
     *
     * @param pos 位置
     * @return 7月8日
     */
    public static String getShowTime(int pos) {
        return shows.get(pos);
    }

}
